import 'dart:math';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';


class CoinRecordController extends GetxController {
  // 数据列表
  List<CoinRecord> items = [];
  
  RefreshController refreshController =
      RefreshController(initialRefresh: false);
      
  @override
  void onInit() {
    super.onInit();
    // 初始化时生成模拟数据
    generateInitialData();
  }
      
  // 生成初始模拟数据
  void generateInitialData() {
    // 生成25条假数据
    items = List.generate(25, (index) {
      final random = Random();
      
      // 随机选择原因
      final reasons = ['充值金币', '观看视频奖励', '签到奖励', '邀请好友奖励', '购买会员', '兑换商品', '抽奖消费'];
      final reason = reasons[random.nextInt(reasons.length)];
      
      // 生成时间
      final now = DateTime.now();
      final pastDate = now.subtract(Duration(hours: index * 3)); 
      final time = "${pastDate.year}.${pastDate.month.toString().padLeft(2, '0')}.${pastDate.day.toString().padLeft(2, '0')} ${pastDate.hour.toString().padLeft(2, '0')}:${pastDate.minute.toString().padLeft(2, '0')}:00";
      
      // 生成金币数量（正数表示充值，负数表示消费）
      final amounts = [100, 200, 500, 1000, -50, -100, -200];
      final amount = amounts[random.nextInt(amounts.length)];
      
      return CoinRecord(
        reason: reason,
        time: time,
        amount: amount,
      );
    });
  }

  void onRefresh() async{
    // 模拟网络请求延迟
    await Future.delayed(Duration(milliseconds: 1000));
    
    // 生成25条假数据
    items = List.generate(25, (index) {
      final random = Random();
      
      // 随机选择原因
      final reasons = ['充值金币', '观看视频奖励', '签到奖励', '邀请好友奖励', '购买会员', '兑换商品', '抽奖消费'];
      final reason = reasons[random.nextInt(reasons.length)];
      
      // 生成时间
      final now = DateTime.now();
      final pastDate = now.subtract(Duration(hours: index * 3)); 
      final time = "${pastDate.year}.${pastDate.month.toString().padLeft(2, '0')}.${pastDate.day.toString().padLeft(2, '0')} ${pastDate.hour.toString().padLeft(2, '0')}:${pastDate.minute.toString().padLeft(2, '0')}:00";
      
      // 生成金币数量（正数表示充值，负数表示消费）
      final amounts = [100, 200, 500, 1000, -50, -100, -200];
      final amount = amounts[random.nextInt(amounts.length)];
      
      return CoinRecord(
        reason: reason,
        time: time,
        amount: amount,
      );
    });
    
    refreshController.refreshCompleted(); // 完成刷新
  }

  void onLoading() async{
    // 模拟网络请求延迟
    await Future.delayed(Duration(milliseconds: 1000));
    
    // 添加更多假数据
    final random = Random();
    for (int i = 0; i < 5; i++) {
      // 随机选择原因
      final reasons = ['充值金币', '观看视频奖励', '签到奖励', '邀请好友奖励', '购买会员', '兑换商品', '抽奖消费'];
      final reason = reasons[random.nextInt(reasons.length)];
      
      // 生成时间
      final now = DateTime.now();
      final pastDate = now.subtract(Duration(hours: (items.length + i) * 3)); 
      final time = "${pastDate.year}.${pastDate.month.toString().padLeft(2, '0')}.${pastDate.day.toString().padLeft(2, '0')} ${pastDate.hour.toString().padLeft(2, '0')}:${pastDate.minute.toString().padLeft(2, '0')}:00";
      
      // 生成金币数量（正数表示充值，负数表示消费）
      final amounts = [100, 200, 500, 1000, -50, -100, -200];
      final amount = amounts[random.nextInt(amounts.length)];
      
      items.add(CoinRecord(
        reason: reason,
        time: time,
        amount: amount,
      ));
    }
    
    refreshController.loadComplete();
  }
}
class CoinRecord {
  final String reason; // 充值或消费原因
  final String time; // 时间
  final int amount; // 金币数量（正数表示充值，负数表示消费）

  CoinRecord({
    required this.reason,
    required this.time,
    required this.amount,
  });
}